package com.youlai.boot.gym.controller;

import com.youlai.boot.gym.service.AccessLogsService;
import com.youlai.boot.system.model.vo.VisitStatsVO;
import com.youlai.boot.system.model.vo.VisitTrendVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.gym.model.form.AccessLogsForm;
import com.youlai.boot.gym.model.query.AccessLogsQuery;
import com.youlai.boot.gym.model.vo.AccessLogsVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.time.LocalDate;

/**
 * 出入日志前端控制层
 *
 * @author hxc
 * @since 2025-06-01 12:37
 */
@Tag(name = "出入日志接口")
@RestController
@RequestMapping("/api/v1/access-logs")
@RequiredArgsConstructor
public class AccessLogsController  {

    private final AccessLogsService accessLogsService;

    @Operation(summary = "出入日志分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('gym:accessLogs:query')")
    public PageResult<AccessLogsVO> getAccessLogsPage(AccessLogsQuery queryParams ) {
        IPage<AccessLogsVO> result = accessLogsService.getAccessLogsPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增出入日志")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('gym:accessLogs:add')")
    public Result<Void> saveAccessLogs(@RequestBody @Valid AccessLogsForm formData ) {
        boolean result = accessLogsService.saveAccessLogs(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取出入日志表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('gym:accessLogs:edit')")
    public Result<AccessLogsForm> getAccessLogsForm(
        @Parameter(description = "出入日志ID") @PathVariable Long id
    ) {
        AccessLogsForm formData = accessLogsService.getAccessLogsFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改出入日志")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('gym:accessLogs:edit')")
    public Result<Void> updateAccessLogs(
            @Parameter(description = "出入日志ID") @PathVariable Long id,
            @RequestBody @Validated AccessLogsForm formData
    ) {
        boolean result = accessLogsService.updateAccessLogs(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除出入日志")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('gym:accessLogs:delete')")
    public Result<Void> deleteAccessLogss(
        @Parameter(description = "出入日志ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = accessLogsService.deleteAccessLogss(ids);
        return Result.judge(result);
    }

    @Operation(summary = "获取当前在场馆内的人数")
    @GetMapping("/getPeopleInGym")
    public Result<Integer> getPeopleInGym() {
        Integer currentMembersInGym = accessLogsService.getPeopleInGym();
        return Result.success(currentMembersInGym);
    }
    @Operation(summary = "获取访问趋势")
    @GetMapping("/visitTrend")
    public Result<VisitTrendVO> getVisitTrend(
            @Parameter(description = "开始时间", example = "yyyy-MM-dd") @RequestParam String startDate,
            @Parameter(description = "结束时间", example = "yyyy-MM-dd") @RequestParam String endDate
    ) {
        LocalDate start = LocalDate.parse(startDate);
        LocalDate end = LocalDate.parse(endDate);
        VisitTrendVO data = accessLogsService.getVisitTrend(start, end);
        return Result.success(data);
    }

    @Operation(summary = "获取访问统计")
    @GetMapping("/visit-stats")
    public Result<VisitStatsVO> getVisitStats() {
        VisitStatsVO result = accessLogsService.getVisitStats();
        return Result.success(result);
    }
}
